package com.jewaton.authmanage.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.jewaton.authmanage.mapper.UserInfoMapper;
import com.jewaton.authmanage.pojo.UserInfo;
import com.jewaton.authmanage.service.IUserInfoService;
import com.jewaton.authmanage.tools.Page;

/**
 * 
 * 用户业务管理实现类
 * @author magiczeng
 *
 */
@Service
public class UserInfoServiceImpl implements IUserInfoService {
	
	private static final Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);
	
	@Autowired
	private UserInfoMapper userInfoMapper;

	/**
	 * 查找用户
	 */
	public Page findUsers(Page page, UserInfo user){
		page = page == null ? new Page() : page;
		List<UserInfo> users = userInfoMapper.findUsersOfPage(page, user);
		page.setList(users);
		return page;
	}
	
	/**
	 * 根据用户ID查找用户
	 */
	@Override
	public UserInfo findUserById(Long userid) {
		if(userid==null)
			return new UserInfo();
		return userInfoMapper.findById(userid);
	} 

	/**
	 * 根据用户名查找用户
	 */
	@Override
	public UserInfo findUserByName(String uname) {
		return userInfoMapper.findByUserName(uname);
	}

	/**
	 * 保存用户信息
	 */
	@Override
	public void saveUserInfo(UserInfo userInfo) {
		if(userInfo==null) 
			return ;
		
		Long userId = userInfo.getId();
		String password = userInfo.getPassword();
		if(StringUtils.hasText(password)){
			Md5PasswordEncoder md5encoder = new Md5PasswordEncoder();
			password = md5encoder.encodePassword(password, null);
		}
		if(userId==null||userId==0){
			//新增
			userInfo.setPassword(password);
			userInfoMapper.insertUserInfo(userInfo);
			logger.info("新增用户成功："+userInfo.getId()+","+userInfo.getUsername()+","+userInfo.getRealname());
		}else{
			//修改
			UserInfo oldUser = this.findUserById(userId);
			BeanUtils.copyProperties(userInfo, oldUser, new String[]{"id","password","authorities","roleList","resourceList"});
			if(StringUtils.hasText(password)){
				oldUser.setPassword(password);
			}
			userInfoMapper.updateUserInfo(oldUser);
			logger.info("更新用户信息成功："+oldUser.getId()+","+oldUser.getUsername()+","+oldUser.getRealname());
		}
		
	}
	
	/**
	 * 更新用户密码
	 * @param userInfo 操作的用户
	 * @param oldPassword  旧密码
	 * @param newPassword 新密码
	 */
	@Override
	public boolean updateUserPassword(UserInfo userInfo, 
			String oldPassword,String newPassword){
		boolean succ = false;
		if(userInfo!=null&&userInfo.getId()!=null&&userInfo.getId()>0
				&&StringUtils.hasText(oldPassword)
				&&StringUtils.hasText(newPassword)){
			Md5PasswordEncoder md5encoder = new Md5PasswordEncoder();
			oldPassword=md5encoder.encodePassword(oldPassword, null);
			if(userInfo.getPassword().equals(oldPassword)){
				newPassword=md5encoder.encodePassword(newPassword, null);
				userInfo.setPassword(newPassword);
				userInfoMapper.updateUserPswd(userInfo);
				succ=true;
				logger.warn(userInfo.getUsername()+"："+userInfo.getRealname()+"修改密码成功！");
			}
			
		}
		
		return succ;
	}
	
	/**
	 * 禁用或启用用户
	 * @param userId 需要禁用或启用的用户ID
	 * @param enabled 是否启用
	 * @return
	 */
	@Override
	public boolean disableUser(Long userId, String enabled){
		boolean succ = false;
		if(userId!=null&&userId>0){
			UserInfo u = new UserInfo("","",null);
			u.setId(userId);
			u.setEnabled("true".equals(enabled)?false:true);
			u.setAccountNonExpired(null);
			u.setAccountNonLocked(null);
			u.setCredentialsNonExpired(null);
			u.setSystemdefine(null);
			userInfoMapper.updateUserInfo(u);
			succ=true;
		}
		return succ;
	}
	
}
